//
//  ViewController.m
//  知乎
//
//  Created by 陶薇 on 2017/10/20.
//  Copyright © 2017年 Liaochengshu. All rights reserved.
//

#import "HomeViewController.h"
#import "HomeTableViewCell.h"
#import "LatestModel.h"
#import "StroriesModel.h"

#define CELLID @"HomeCell"

@interface HomeViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) UITableView *tableView;
//添加一个可变的数据源
@property (nonatomic,strong)NSMutableArray *datas;
@end

@implementation HomeViewController
#pragma mark  - 懒加载
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        //去掉cell线
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        //注册
        [_tableView registerClass:[HomeTableViewCell class] forCellReuseIdentifier:CELLID];
    }
    return _tableView;
}
   - (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
       [self.view addSubview:self.tableView];
       [self getLatest];
}
#pragma mark - 获取新闻数据
- (void)getLatest
{
    [[AFHTTPSessionManager manager] GET:@"https://news-at.zhihu.com/api/4/news/latest" parameters:nil progress:nil
 success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
    // NSLog(@"%@",responseObject);
     //指定一下stories里面放StroriesModel
     [LatestModel mj_setupObjectClassInArray:^NSDictionary *{
         return @{
                  @"stories":@"StroriesModel"
                  };
     }];
     //mj的字典转模型
     LatestModel *model = [LatestModel mj_objectWithKeyValues:responseObject];
     
     self.datas = model.stories.mutableCopy;
    // NSLog(@"");
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    
}] ;
}
#pragma mark - UITableView Delegate & DateSource
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [self.tableView cellHeightForIndexPath:indexPath model:self.datas[indexPath.row]keyPath:@"model" cellClass:[HomeTableViewCell class] contentViewWidth:self.view.frame.size.width];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.datas.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CELLID];
    cell.model = self.datas[indexPath.row];
    return cell;
}
//重写取方法，刷新tableView 
- (void)setDatas:(NSMutableArray *)datas
{
    _datas = datas;
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
